<template>
	<scroll-view @scrolltolower="onLower" @refresherrefresh="onRefresh" refresher-enabled="true"
		:refresher-threshold="40" :refresher-triggered="triggered" scroll-y="true"
		:style="{height: pageHeight - 40 + 'px'}">
		<uni-card v-for="(item,index) in listData" @click="openOperate(item)" :key="index" spacing="0"
			:is-shadow="false" :border="false" class="layout-dom">
			<view class="slot-title ">
				<view class="flex-row flex-row-space-between">
					<text class="title">{{item.projectName}}</text>
					<uni-icons type="more-filled" size="20" color="#999"></uni-icons>
				</view>
			</view>
			<view class="flex-row item-dom">
				<view class="title">
					<text class="iconfont icon-gongsi" style="padding-right: 15rpx; color: #01c2b7"></text>
					<text>所属企业：</text>
				</view>
				<view class="content">{{item.projectEnterprise}}</view>
			</view>
			<view class="flex-row item-dom">
				<view class="title">
					<text class="iconfont icon-gangwei" style="padding-right: 15rpx; color: #01c2b7"></text>
					<text>所属岗位：</text>
				</view>
				<view class="content">{{item.projectPosition}}</view>
			</view>
			<view class="flex-row item-dom">
				<view class="title">
					<text class="iconfont icon-describe" style="padding-right: 15rpx; color: #01c2b7"></text>
					<text>工作内容：</text>
				</view>
				<view class="content">{{item.projectDescribe}}</view>
			</view>
			<view class="flex-row item-dom">
				<view class="title">
					<text class="iconfont icon-wodechengguo" style="padding-right: 15rpx; color: #01c2b7"></text>
					<text>工作成果：</text>
				</view>
				<view class="content">{{item.projectAchievement}}</view>
			</view>
		</uni-card>
	</scroll-view>

	<uinloadmore :status="status" />

	<acOperation ref="operationRef" @operateClick="operateClick" />

</template>

<script lang="ts" setup>
	import acOperation from "@/pages/student/user/a-components/ac-operation.vue"
	import uinloadmore from "@/components/uni/uni-load-more"
	import {getUser } from "@/utils/storage"
	import { Project } from "@/api/tables/Project"
	import { User } from "@/api/tables/User"
	import { projectPage, projectDelete } from "@/api/user"
	import { ref,  onBeforeMount } from 'vue'

	let operationRef = ref(null)
	let pageHeight = ref(90)
	let userData = ref<User>({})
	let total = ref(0)
	let pagination = ref({ current: 1, size: 10 })
	let triggered = ref<boolean>(false)
	let status = ref('noMore')
	let listData = ref<Array<Project>>([])
	let selectItem = ref<Project>({})

	onBeforeMount(() => {
		listData.value = []
		userData.value = getUser()
		triggered.value = false
		pageHeight.value = uni.getSystemInfoSync().windowHeight; // 获取当前窗口高度
		uni.onWindowResize((res) => { // 监听窗口大小的变化
			pageHeight.value = res.size.windowHeight;
		})
		getListData()
	})
	// 上拉加载
	function onLower() {
		if (total.value <= listData.value.length) {
			status.value = 'noMore'
		} else {
			status.value = 'loading'
			pagination.value.current++
			getListData()
		}
	}
	// 下拉刷新
	function onRefresh() {
		pagination.value.current = 1
		triggered.value = true
		getListData()
	}
	// 获取列表数据
	function getListData() {
		projectPage({
			jobseekerId: userData.value.userId,
			...pagination.value
		}).then(res => {
			if (res.code == '200') {
				let list = res.data.records
				listData.value = triggered.value ? list : listData.value.concat(list)
				total.value = res.data.total
				status.value = res.data.total <= listData.value.length ? 'noMore' : 'more'
				triggered.value = false
			}
		}).finally( ()  => {
			triggered.value = false
		})
	}
	// 弹出 操作层
	function openOperate(item) {
		selectItem.value = item
		operationRef.value.openOperate(item.activityName)
	}
	// 关闭 操作层
	function closeOperate() {
		operationRef.value.closeOperate()
	}
	// 操作
	function operateClick(key) {
		if (key == 'delete') {
			deleteOK(selectItem.value.id)
		} else if (key == 'edit') {
			uni.navigateTo({
				url: './edit?itemID=' + selectItem.value.id + "&jobseekerId=" + selectItem.value.jobseekerId,
				success: () => {
					closeOperate()
					uni.$once('editUpdate', data => {
						listData.value = listData.value.map(t => {
							return t.id === data.id ? data : t;
						});
						console.log('编辑成功返回')
					})
				}
			})
		} else if (key == 'details') {
			uni.navigateTo({
				url: './details?itemID=' + selectItem.value.id + "&jobseekerId=" + selectItem.value.jobseekerId,
				success: () => {
					closeOperate()
				}
			});
		} else if (key == 'add') {
			uni.navigateTo({
				url: './add',
				success: () => {
					closeOperate()
					uni.$once('addUpdate', data => {
						listData.value.unshift(data)
						total.value++
						console.log('新增成功返回')
					})
				}
			})
		}
	}
	// 删除
	function deleteOK(id) {
		projectDelete([{ id }]).then(res => {
			if (res.code == '200') {
				let list = listData.value.filter(item => item.id != id);
				listData.value = list
				operationRef.value.closeOperate()
			}
		})
	}
</script>

<style lang="scss" scoped>
	.item-dom {
		font-size: 28rpx;
		color: #999999;
		font-family: "微软雅黑";

		.title {
			width: 100px;
			color: #131313;
		}

		.content {
			max-width: calc(100% - 100px);
		}
	}
</style>